/*
 * Huawei Software Technologies Co., Ltd. Copyright 1998-2009, All rights reserved.
 * 文件名  :CtmsSoapBindingStub.java
 * 创建人  :yuebinyao
 * 创建时间:2013-3-18
 */
package com.cimp.service.c2.server;

import java.net.URL;
import java.rmi.RemoteException;
import java.util.Enumeration;
import java.util.Vector;
import javax.xml.namespace.QName;
import org.apache.axis.AxisFault;
import org.apache.axis.NoEndPointException;
import org.apache.axis.client.Call;
import org.apache.axis.client.Stub;
import org.apache.axis.constants.Style;
import org.apache.axis.constants.Use;
import org.apache.axis.description.OperationDesc;
import org.apache.axis.description.ParameterDesc;
import org.apache.axis.encoding.DeserializerFactory;
import org.apache.axis.encoding.ser.BeanDeserializerFactory;
import org.apache.axis.encoding.ser.BeanSerializerFactory;
import org.apache.axis.soap.SOAPConstants;
import org.apache.axis.utils.JavaUtils;

/**
 * 
 * [简要描述]:<br/>
 * [详细描述]:<br/>
 *
 * @author yuebinyao
 * @version [revision],Mar 19, 2013
 * @since IPTV-CIMP-V100R003C02B760
 */
public class CtmsSoapBindingStub extends Stub implements CSPResponse
{
    private Vector cachedSerClasses = new Vector();

    private Vector cachedSerQNames = new Vector();

    private Vector cachedSerFactories = new Vector();

    private Vector cachedDeserFactories = new Vector();

    static OperationDesc[] _operations = new OperationDesc[1];

    static
    {
        initOperationDesc1();
    }
    
    /**
     * 
     * [简要描述]:<br/>
     * [详细描述]:<br/>
     *
     * @throws AxisFault AxisFault
     */
    public CtmsSoapBindingStub() throws AxisFault
    {
        this(null);
    }

    /**
     * 
     * [简要描述]:<br/>
     * [详细描述]:<br/>
     *
     * @param endpointURL URL
     * @param service javax.xml.rpc.Service
     * @throws AxisFault AxisFault
     */
    public CtmsSoapBindingStub(URL endpointURL, javax.xml.rpc.Service service) throws AxisFault
    {
        this(service);
        this.cachedEndpoint = endpointURL;
    }

    /**
     * 
     * [简要描述]:<br/>
     * [详细描述]:<br/>
     *
     * @param service javax.xml.rpc.Service javax.xml.rpc.Service
     * @throws AxisFault AxisFault AxisFault
     */
    public CtmsSoapBindingStub(javax.xml.rpc.Service service) throws AxisFault
    {
        if (null == service)
        {
            this.service = new org.apache.axis.client.Service();
        }
        else
        {
            this.service = service;
        }
        ((org.apache.axis.client.Service) this.service).setTypeMappingVersion("1.2");

        Class beansf = BeanSerializerFactory.class;
        Class beandf = BeanDeserializerFactory.class;

        QName qName = new QName("iptv", "CSPResult");
        this.cachedSerQNames.add(qName);
        Class cls = CSPResult.class;
        this.cachedSerClasses.add(cls);
        this.cachedSerFactories.add(beansf);
        this.cachedDeserFactories.add(beandf);
    }

    /**
     * 
     * [简要描述]:<br/>
     * [详细描述]:<br/>
     * 
     * @exception
     */
    private static void initOperationDesc1()
    {
        OperationDesc oper = new OperationDesc();
        oper.setName("ResultNotify");
        ParameterDesc param = new ParameterDesc(new QName("", "CSPID"), (byte) 1, new QName(
                "http://schemas.xmlsoap.org/soap/encoding/", "string"), String.class, false, false);
        oper.addParameter(param);
        param = new ParameterDesc(new QName("", "LSPID"), (byte) 1, new QName(
                "http://schemas.xmlsoap.org/soap/encoding/", "string"), String.class, false, false);
        oper.addParameter(param);
        param = new ParameterDesc(new QName("", "CorrelateID"), (byte) 1, new QName(
                "http://schemas.xmlsoap.org/soap/encoding/", "string"), String.class, false, false);
        oper.addParameter(param);
        param = new ParameterDesc(new QName("", "CmdResult"), (byte) 1, new QName("http://www.w3.org/2001/XMLSchema",
                "int"), Integer.TYPE, false, false);
        oper.addParameter(param);
        param = new ParameterDesc(new QName("", "ResultFileURL"), (byte) 1, new QName(
                "http://schemas.xmlsoap.org/soap/encoding/", "string"), String.class, false, false);
        oper.addParameter(param);
        oper.setReturnType(new QName("iptv", "CSPResult"));
        oper.setReturnClass(CSPResult.class);
        oper.setReturnQName(new QName("", "ResultNotifyReturn"));
        oper.setStyle(Style.RPC);
        oper.setUse(Use.ENCODED);
        _operations[0] = oper;
    }



    /**
     * 
     * [简要描述]:<br/>
     * [详细描述]:<br/>
     * 
     * @return Call
     * @throws RemoteException RemoteException
     * @exception
     */
    protected Call createCall() throws RemoteException
    {
        Call call = null;
        Enumeration keys = null;
        String key = null;
        Class cls = null;
        QName qName = null;
        Object x = null;
        Class csf = null;
        Class cdf = null;
        org.apache.axis.encoding.SerializerFactory sf = null;
        DeserializerFactory df = null;
        
        try
        {
            call = super._createCall();
            if (this.maintainSessionSet)
            {
                call.setMaintainSession(this.maintainSession);
            }
            if (this.cachedUsername != null)
            {
                call.setUsername(this.cachedUsername);
            }
            if (this.cachedPassword != null)
            {
                call.setPassword(this.cachedPassword);
            }
            if (this.cachedEndpoint != null)
            {
                call.setTargetEndpointAddress(this.cachedEndpoint);
            }
            if (this.cachedTimeout != null)
            {
                call.setTimeout(this.cachedTimeout);
            }
            if (this.cachedPortName != null)
            {
                call.setPortName(this.cachedPortName);
            }
            keys = this.cachedProperties.keys();
            while (keys.hasMoreElements())
            {
                key = (String) keys.nextElement();
                call.setProperty(key, this.cachedProperties.get(key));
            }

            synchronized (this)
            {
                if (firstCall())
                {
                    call.setSOAPVersion(SOAPConstants.SOAP11_CONSTANTS);
                    call.setEncodingStyle("http://schemas.xmlsoap.org/soap/encoding/");
                    for (int i = 0; i < this.cachedSerFactories.size(); i++)
                    {
                        cls = (Class) this.cachedSerClasses.get(i);
                        qName = (QName) this.cachedSerQNames.get(i);
                        x = this.cachedSerFactories.get(i);
                        if (x instanceof Class)
                        {
                            csf = (Class) this.cachedSerFactories.get(i);
                            cdf = (Class) this.cachedDeserFactories.get(i);
                            call.registerTypeMapping(cls, qName, csf, cdf, false);
                        }
                        else if (x instanceof javax.xml.rpc.encoding.SerializerFactory)
                        {
                            sf = (org.apache.axis.encoding.SerializerFactory) this.cachedSerFactories.get(i);
                            df = (DeserializerFactory) this.cachedDeserFactories.get(i);
                            call.registerTypeMapping(cls, qName, sf, df, false);
                        }
                    }
                }
            }
            return call;
        }
        catch (Throwable t)
        {
            throw new AxisFault("Failure trying to get the Call object", t);
        }
    }

    /**
     * 
     * [简要描述]:<br/>
     * [详细描述]:<br/>
     * 
     * @param cspId String
     * @param lspId String
     * @param correlateID String
     * @param cmdResult int
     * @param resultFileURL String
     * @return  CSPResult
     * @throws RemoteException RemoteException
     * @exception 
     * @see com.huawei.iptv.cms.smg.soap.client.CSPResponse#
     * resultNotify(java.lang.String, java.lang.String, java.lang.String, int, java.lang.String)
     */
    @Override
    public CSPResult resultNotify(String cspId, String lspId, String correlateID, int cmdResult, String resultFileURL)
        throws RemoteException
    {
        if (this.cachedEndpoint == null)
        {
            throw new NoEndPointException();
        }
        
        Call call = createCall();
        call.setOperation(_operations[0]);
        call.setUseSOAPAction(true);
        call.setSOAPActionURI("");
        call.setSOAPVersion(SOAPConstants.SOAP11_CONSTANTS);
        call.setOperationName(new QName("iptv", "ResultNotify"));
        setRequestHeaders(call);
        setAttachments(call);
        
        Object resp = null;
        try
        {
            resp = call.invoke(new Object[]{
                cspId, lspId, correlateID, Integer.valueOf(cmdResult), resultFileURL});

            if (resp instanceof RemoteException)
            {
                throw (RemoteException) resp;
            }

            extractAttachments(call);
            try
            {
                return (CSPResult) resp;
            }
            catch (Exception exception)
            {
                return (CSPResult) JavaUtils.convert(resp, CSPResult.class);
            }
        }
        catch (AxisFault axisFaultException)
        {
            throw axisFaultException;
        }
    }
}
